package com.china.produce.strategy.chain.execute;

import com.china.produce.strategy.chain.AbstractDetect;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 处理逻辑的工厂
 *
 * @ClassName
 * @Author niujie
 * @Version
 * @Description
 * @CreateTime 2024/7/21
 */
@Component
public class DealLogicFactory implements ApplicationContextAware {

    /**
     * 设置applicationContext
     */
    private ApplicationContext applicationContext;

    /**
     * 获取实现类
     */
    private List<AbstractDetect> beans;

    /**
     * 获取实现接口所有类，并且排序
     */
    @PostConstruct
    public void DealLogicFactory()
    {

        Map<String, AbstractDetect> beansOfType = applicationContext.getBeansOfType(AbstractDetect.class);

        this.beans = beansOfType.values().stream().sorted(Comparator.comparing(AbstractDetect::sort)).collect(Collectors.toList());
    }


    public List<AbstractDetect> getBeans()
    {

        return this.beans;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
    {

        this.applicationContext = applicationContext;
    }
}
